﻿
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SignalrClientWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
       
        public  const string token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjU1RTM2QTEwMjhCQUQ5MUIzODM1NTU5NEM3MEY4MUEwQjkyOEUwNDBSUzI1NiIsInR5cCI6Imp3dCIsIng1dCI6IlZlTnFFQ2k2MlJzNE5WV1V4dy1Cb0xrbzRFQSJ9.eyJuYmYiOjE1OTc5MjE0NjQsImV4cCI6MTU5NzkyNTA2NCwiaXNzIjoiaHR0cHM6Ly9zay0yMDIwMDQwMXZvamUuZG9tYWluMjM3Lm9yZzo1MDgwIiwiYXVkIjpbImFwaTIiLCJhcGkxIl0sImNsaWVudF9pZCI6InZ1ZV9qc19jbGllbnQiLCJzdWIiOiJTLTEtNS0yMS0yNTgwMDkxMjkzLTM4OTI5ODg5ODctMjMwNTY2NTY3OC0yNjA2IiwiYXV0aF90aW1lIjoxNTk3OTE3OTIzLCJpZHAiOiJsb2NhbCIsIm5hbWUiOiJQQy0wMDUiLCJlbWFpbCI6InNreSIsImRucyI6IiouZG9tYWluMjM3Lm9yZyIsIng1MDBkaXN0aW5ndWlzaGVkbmFtZSI6IkU9c2t5LCBDTj1QQy0wMDUsIE9VPXdhbmdkb3UsIFM9c2hhbnhpLCBDPWNuIiwianRpIjoiMkMxREEyN0U5RjA4QjBDNEM4MkFCRTE2NTgwOTgzMTMiLCJpYXQiOjE1OTc5MjE0NjQsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJyZWFkIiwib2ZmbGluZV9hY2Nlc3MiXSwiYW1yIjpbInB3ZCJdfQ.pEdUehqzLvX8DRYj1Ie3A7outQR4G3PXdq67uT0fnJQlc01L9aidL4aghwkFOOVn7cxU2hwM4ZLpSFJyemiKhFiYs1lQxjZaLhqIqJLSp0D9VbJAvz0g9ARkAEFq_RX5jhHASaiVGSX86A4e-xWO9Qrlr-pHveePyYPIfqNMydh1Yf7AH_RWkEYWTD_F96BsaRrsJ8oZslbK8r9-13ceGIZC_s_Vo-yEjZeacn8BIAILbdNCEnjCm_vE5JAUu5VO9YnKSvNZskE5_U6ct-aiKyKAV0oa5wMvPPWgSjFRUgblcIYk15HRh4R6ZX7oF5r8UMK-0F_y6RRfoR7cnZmijg";

        SignalrClient signalrClient;

        public MainWindow()
        {
            InitializeComponent();
            signalrClient = new SignalrClient(token);
            signalrClient.Start((message, group) => {
                messagesList.Items.Add(message);
                Console.WriteLine(message);
            });

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(async () =>
            {
                await signalrClient.Connection.SendAsync("send","122333");
            });
            
        }

        private void sendButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }


    public class SignalrClient
    {
       
        internal HubConnection Connection;
        public string Token { get; }
        public SignalrClient(string token)
        {
            Token = token;
        }
        public void Start(Action<string, string> action)
        {
            Console.WriteLine("Signarl start begin...");
            if (action == null)
            {
                Console.WriteLine (" action is null ");
            }
          
            Connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:5000/GeneralHub", options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(Token);
            })
            .WithAutomaticReconnect()
            .Build();

            Connection.Closed += this.Connection_Closed;
            Connection.Reconnecting += this.Connection_Reconnecting;

            Connection.On("ReceiveMessage", action);

            try
            {
                Task.Run(async () =>
                {
                    await Connection.StartAsync();
                    Console.WriteLine("Signarl start end...");
                });

            }
            catch (System.Exception ex)
            {
               
            }
        }

        private Task Connection_Reconnecting(Exception arg)
        {
          
            //重新链接
            return Task.CompletedTask;
        }

        private Task Connection_Closed(Exception arg)
        {
           
            var closedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
            closedTcs.SetResult(null);
            return Task.CompletedTask;
        }



    }

}
